<template>
  <div class="set-bg">

    <div class="weui-cell weui-cell_switch" v-if="isWX">
      <div class="weui-cell__bd">开启微信通知</div>
      <div class="weui-cell__ft">
        <input class="weui-switch" type="checkbox" @change="wxClick" v-model="isWxNotice"/>
      </div>
    </div>


    <router-link :to="{name:'changePassword'}" class="weui-cell weui-cell_access">
      <div class="weui-cell__bd">
        <p>修改密码</p>
      </div>
      <div class="weui-cell__ft">
      </div>
    </router-link>

    <!--<div class="logoff" @click="deleteClick">-->
      <!--注销账号-->
    <!--</div>-->

    <div class="logoff" @click="logOff">
      退出登录
    </div>



  </div>
</template>

<script>
  import {getStatusMapByKey,clearStore,getUser,setUser,removelocalStore,isWeiXin} from '../../../assets/js/tools';
  import {getUserMsg,updateWxnotice,doLoginOut,deleteUser} from '../../../assets/js/getData'
  export default {
    name: "setUp",
    data(){
      return {
        user:null,
        isWX:null,

        isWxNotice:null,//是否接受微信通知

        eBooleanStatus:{},
      }
    },
    created(){
      this.eBooleanStatus = getStatusMapByKey('eBooleanStatus');
      this.isWX = isWeiXin();
      this.user = getUser();

      let isWxNotice = this.user.isWxNotice;
      if(isWxNotice==this.eBooleanStatus['yes'].val){
        this.isWxNotice = true;
      }else if(isWxNotice==this.eBooleanStatus['no'].val){
        this.isWxNotice = false;
      }

    },
    methods:{
      //刷新用户信息
      async getUserMsg(){
        let res = await getUserMsg();
        if(res.data.reqResult.code>0){
          let isWxNotice = res.data.data.user.isWxNotice;
          this.user.isWxNotice = isWxNotice;
          setUser(this.user);
        }else {
          Toast(res.data.reqResult.msg);
        }
      },
      //是否接受微信通知
      async wxClick(){
        let isWxNotice;
        if(this.isWxNotice){
          isWxNotice = this.eBooleanStatus['yes'].val;
        }else {
          isWxNotice = this.eBooleanStatus['no'].val;
        }
        let res = await updateWxnotice(this.user.id,isWxNotice);
        if(res.data.reqResult.code>0){
          this.getUserMsg();
        }else {
          Toast(res.data.reqResult.msg);
        }
      },
      //退出登录
      async logOff(){
        let res = await doLoginOut();
        if(res.data.reqResult.code>0){
          clearStore();
          removelocalStore('user');
          this.$router.replace({name:'home'});
        }else {
          Toast(res.data.reqResult.msg);
        }
      },
      //点击注销用户按钮
      deleteClick(){
        MessageBox.confirm('注销之后该账户将无法登录，确定要注销该账号吗？','').then(() => {
          deleteUser().then(res => {
            if(res.data.reqResult.code>0){
              clearStore();
              removelocalStore('user');
              this.$router.replace({name:'home'});
            }else {
              Toast(res.data.reqResult.msg);
            }
          })
        }).catch(()=>{})
      },

    }
  }
</script>

<style scoped>
  .set-bg{
    min-height: 100vh;
    background-color: #F4F4F4;
  }
  .weui-cell{
    background-color: #FFFFFF;
    height: 2.5rem;
    line-height: 2.5rem;
    padding: 0 0.75rem;
  }
  .weui-cell p{
    font-size: 0.75rem;
    color: #333333;
  }
  .weui-cell_access .weui-cell__ft:after{
    color: #D2D2D2;
  }
  .weui-cell__ft{
    display: flex;
  }
  .logoff{
    background-color: #FFFFFF;
    height: 2.5rem;
    line-height: 2.5rem;
    text-align: center;
    color: #C30000;
    font-size: 0.75rem;
    margin-top: 0.5rem;
  }
  .weui-switch:checked, .weui-switch-cp__input:checked ~ .weui-switch-cp__box{
    border-color: #C50000;
    background-color: #C50000;
  }
  .weui-cell_switch{
    margin-bottom: 0.5rem;
  }
  .weui-cell_switch .weui-cell__bd{
    font-size: 0.75rem;
    color: #333333;
  }
</style>
